<?php

/**
 * Description of EventController Zend_Controller_Action
 *  
 * 
 * @author christiangreweling, Sascha Witte
 */
class EventController extends Zend_Controller_Action {

    //put your code here
    protected $eventMapper;
    protected $locationMapper;
    protected $catanMapper;

    public function init() {
        $this->_statusMessenger = $this->_helper->flashMessenger;
        $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
        $this->_errorMessenger->setNamespace('error');
        $this->view->statusMessages = $this->_statusMessenger->getMessages();
        $this->view->errorMessages = $this->_errorMessenger->getMessages();

        $this->eventMapper = new Application_Model_EventMapper();
        $this->locationMapper = new Application_Model_EventMapper();

        $input = new Zend_Filter_Input($filters, $validators,
                        $this->_request->getPost());
        $input->setDefaultEscapeFilter(
                new Zend_Filter_HtmlEntities(ENT_COMPAT, 'utf-8')
        );
        //Datum auf Locale setzen
        $locale = new Zend_Locale();
        $language = $locale->getLanguage();
        if ($language == 'de') {
            $this->view->headScript()->appendFile('/resources/jscript/datepicker_Localization/jquery.ui.datepicker-de.js');
        } else {
            $this->view->headScript()->appendFile('/resources/jscript/datepicker_Localization/jquery.ui.datepicker-en.js');
        }
    }

    public function indexAction() {

        $this->view->documentClasses = array('event');
        $this->view->events = $this->eventMapper->fetchAll(null, "date DESC");
    }

    /**
     * Zeigt alle Spiele eines Spielabends
     * 
     * Spielhinzufuegen weiterleitung auf game/add 
     */
    public function detailAction() {
        if ($this->getRequest()->isGet()) {
            $eventId = (int) $this->getRequest()->getParam('id');

            $eventModel = new Application_Model_EventModel();
            $this->view->event = $this->eventMapper->find($eventId, $eventModel);

            $catanMapper = new Application_Model_ExplicitCatanMapper();
            $where = 'event_id =' . $eventId;
            $catanGames = $catanMapper->fetchAll($where);
            $this->view->games = $catanGames;
        }
        if ($this->getRequest()->isPost()) {
            $blogId = (int) $this->getRequest()->getParam('id');
        }
        if ($blogId != null) {
            
        }
    }

    /**
     * hinzufuegen eines Events das per Get übergeben wurde
     * 
     * Auswahl von Spielern und Spieltyp
     * 
     * redirect zu /event
     */
    public function addEventAction() {
        $this->view->headScript()->setAllowArbitraryAttributes(true);
        $this->view->headTitle('Create new Event');
        $this->view->form = $this->_getAddEventForm();
        $post = $this->_request->getPost();
        if ($post) {

            $values = $this->_request->getParams();

            $event = new Application_Model_EventModel();
            $event->setDate($values['date'])
                    ->setLocation_id($values['location']);
            $event = $this->eventMapper->save($event);

            $this->_redirect('/event/detail?id=' . $event->getId());
        }
    }

    /*
     * Erstellt AddEventForm
     * 
     * @return $form Zend_Form  
     */

    protected function _getAddEventForm() {
        $form = new Zend_Form();
        $form->setMethod(Zend_Form::METHOD_POST);

        $date = new Zend_Date();

        //herausfinden weche Sprache benutzt wird
        $locale = new Zend_Locale();
        $language = $locale->getLanguage();


        $date->set(Zend_Date::now(), 'dd.MM.YY');
        $element = new ZendX_JQuery_Form_Element_DatePicker('date',
                        array('jQueryParams' => array('dateFormat' => 'yy-mm-dd'),
                            'label' => 'Date', 'value' => $date->get('yyyy-MM-dd'),
                            'regional' => $language,
                            'size' => 29)
        );


        $form->addElement($element);


        $locationSelect = new Zend_Form_Element_Select('location');
        $locationSelect->setLabel('Location')
               // ->setAttrib('style', 'width: 200px;')
                ->setRequired(true)->addValidator('NotEmpty', true);

        $table = new Application_Model_LocationMapper();
        $table = $table->fetchAll(null, "id DESC");
        foreach ($table as $c) {
            $locationSelect->addMultiOption($c->getId(), $c->getName());
        }

        $form->addElement($locationSelect);

        $form->addElements(array(array('submit', 'submit', array(
                    'label' => _('save'),
                    'decorators' => array('ViewHelper')
            ))));

        return $form;
    }

}

?>
